suppressMessages(library(plotly))
suppressMessages(library(tidyverse))
library(readr)
leave <- read_csv(here::here('dataset','leave.csv')) %>%
rename(State = o_state_name) %>%
mutate(State = toupper(State))
## Rows: 51 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): o_state_name
## dbl (3): leave_pop, total_pop, proportion
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
leave$State <- gsub(' ','',leave$State)
movein <- read_csv(here::here('dataset','movein.csv')) %>%
rename(State = d_state_name) %>%
mutate(State = toupper(State))
## Rows: 51 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): d_state_name
## dbl (3): movein_pop, total_pop, proportion
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
movein$State <- gsub(' ','',movein$State)
# crime rate vs leave proportion by year
leave_crime <- read_csv(here::here('dataset','00-07Crime.csv')) %>%
rename(State=Area)%>%
inner_join(leave,by='State')
## Rows: 358 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Area
## dbl (3): Robbery, Burglary, year
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
leave_crime %>%
pivot_longer(c(Robbery,Burglary),names_to="crime",values_to="value")%>%
ggplot(aes(x=value,y=proportion,color=crime)) +
geom_point() +
facet_grid(vars(year))
leave_crime<- leave_crime %>%
pivot_longer(c(Robbery,Burglary),names_to="crime",values_to="value")
g <- ggplot(leave_crime, aes(x = value, y = proportion, color = crime,
text = paste('population left',leave_pop, "<br>", 'total population',total_pop))) +
geom_point(position = "jitter")+
facet_grid(vars(year))
ggplotly(g, tooltip = "text")
# crime rate vs movein proportion by year
movein_crime <- read_csv(here::here('dataset','10-17Crime.csv')) %>%
inner_join(movein,by='State')
## Rows: 400 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): State
## dbl (3): Robbery, Burglary, year
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
movein_crime %>%
pivot_longer(c(Robbery,Burglary),names_to="crime",values_to="value")%>%
ggplot(aes(x=value,y=proportion,color=crime)) +
geom_point() +
facet_grid(vars(year))
movein_crime<- movein_crime %>%
pivot_longer(c(Robbery,Burglary),names_to="crime",values_to="value")
f <- ggplot(movein_crime, aes(x = value, y = proportion, color = crime,
text = paste('population moved in',movein_pop, "<br>", 'total population',total_pop))) +
geom_point(position = "jitter")+
facet_grid(vars(year))+labs(x = 'Crime rate per 100,000 inhabitants',y = 'Proportion of people left', title = 'Crime rate vs. Proportion of people left')
ggplotly(f, tooltip = "text")
library(leaflet)
library(tidyverse)
library(plotly)
library(DT)
library(readr)
library(crosstalk)
# To explore the correlation between crime rate and proportion of people moved
# into/ leave the states by year
movein1 <- read_csv("dataset/movein.csv")
## Rows: 51 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): d_state_name
## dbl (3): movein_pop, total_pop, proportion
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
leave1 <- read_csv("dataset/leave.csv")
## Rows: 51 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): o_state_name
## dbl (3): leave_pop, total_pop, proportion
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
movein1$d_state_name <- toupper(movein1$d_state_name)
leave1$o_state_name <- toupper(leave1$o_state_name)
early <- read_csv(here::here('dataset','00-07CrimeAvg.csv'))
## Rows: 52 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Area
## dbl (2): meanRobbery, meanBurglary
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
early<- early %>% filter(Area!= 'DISTRICTOFCOLUMBIA')
later <- read_csv(here::here('dataset','10-17CrimeAvg.csv'))
## Rows: 50 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): State
## dbl (2): meanRobbery, meanBurglary
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
colnames(movein1)[1] <- "Area"
colnames(leave1)[1] <- "Area"
colnames(early)[2] <- "early_meanRobbery"
colnames(early)[3] <- "early_meanBurglary"
colnames(later)[2] <- "later_meanRobbery"
colnames(later)[3] <- "later_meanBurglary"
colnames(later)[1] <- "Area"
movein1 <- movein1 %>% filter(Area != 'DC') %>% select(Area,proportion)
leave1 <- leave1 %>% filter(Area != 'DC')%>% select(Area,proportion)
colnames(movein1)[2] <- "prop_movein"
colnames(leave1)[2] <- "prop_leave"
for_interactive <- read_csv("dataset/us map.csv")
## Rows: 50 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Place Name
## dbl (2): Latitude, Longitude
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
colnames(for_interactive)[1] <- "Area"
for_interactive$Area <- gsub("(.*),.*", "\\1", for_interactive$Area) %>% toupper()
for_interactive<- for_interactive %>% inner_join(movein1,by = 'Area')
for_interactive<- for_interactive %>% inner_join(leave1,by = 'Area')
for_interactive$Area<-str_replace_all(for_interactive$Area, " ", "")
for_interactive<- for_interactive %>% inner_join(early,by = 'Area')
for_interactive<- for_interactive %>% inner_join(later,by = 'Area')
d <- SharedData$new(for_interactive[sample(nrow(for_interactive), 50),])
# Create a filter input
filter_slider("prop_movein", "movein", d, column=~prop_movein, step=0.1, width=250)
# Use SharedData like a dataframe with Crosstalk-enabled widgets
bscols(
leaflet(d) %>% addTiles() %>% addMarkers(),
datatable(d, extensions="Scroller", style="bootstrap", class="compact", width="100%",
options=list(deferRender=TRUE, scrollY=300, scroller=TRUE))
)
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
a1 <- for_interactive %>% pivot_longer(c(early_meanRobbery,early_meanBurglary),names_to = 'crimeType',values_to = 'crimeRate')
plot_ly(a1, type = "scatter", mode = "markers",
x = ~jitter(crimeRate, 2), y = ~jitter(prop_leave,2), color = ~crimeType,
text = ~paste(Area, "<br>", prop_leave),
hovertemplate = "%{text}") %>%
layout(xaxis = list(title = "Early years crime rate"), yaxis = list(title = "proportion of people moved out of State"))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
a1 <- for_interactive %>% pivot_longer(c(later_meanRobbery,later_meanBurglary),names_to = 'crimeType',values_to = 'crimeRate')
plot_ly(a1, type = "scatter", mode = "markers",
x = ~jitter(crimeRate, 2), y = ~jitter(prop_movein,2), color = ~crimeType,
text = ~paste(Area, "<br>", prop_movein),
hovertemplate = "%{text}") %>%
layout(xaxis = list(title = "Later years crime rate"), yaxis = list(title = "proportion of people moved into the State"))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
```